
package com.android.fitnesscenter.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

public class Loger {

    private static boolean LOGER = true;
    private static File filesDir;
    private static File logFile;
    private static final String FORMAT_STR = "[%s][%d]%s";
    private static boolean isSave = false;

    public static void v(String msg) {
        if (!LOGER && !isSave) return;
        final StackTraceElement[] stack = new Throwable().getStackTrace();
        final int i = 1;
        final StackTraceElement ste = stack[i];
        String logstr = String.format(FORMAT_STR, ste.getMethodName(), ste.getLineNumber(), msg);
        if (LOGER) {
            Log.v(ste.getClassName(), logstr);
        }
        if (isSave) {
            appendLog(logstr);
        }
    }

    public static void d(String msg) {
        if (!LOGER && !isSave) return;
        final StackTraceElement[] stack = new Throwable().getStackTrace();
        final int i = 1;
        final StackTraceElement ste = stack[i];
        String logstr = String.format(FORMAT_STR, ste.getMethodName(), ste.getLineNumber(), msg);
        if (LOGER) {
            Log.d(ste.getClassName(), logstr);
        }
        if (isSave) {
            appendLog(logstr);
        }
    }

    public static void i(String msg) {
        if (!LOGER && !isSave) return;
        final StackTraceElement[] stack = new Throwable().getStackTrace();
        final int i = 1;
        final StackTraceElement ste = stack[i];
        String logstr = String.format(FORMAT_STR, ste.getMethodName(), ste.getLineNumber(), msg);
        if (LOGER) {
            Log.i(ste.getClassName(), logstr);
        }
        if (isSave) {
            appendLog(logstr);
        }
    }

    public static void w(String msg) {
        if (!LOGER && !isSave) return;
        final StackTraceElement[] stack = new Throwable().getStackTrace();
        final int i = 1;
        final StackTraceElement ste = stack[i];
        String logstr = String.format(FORMAT_STR, ste.getMethodName(), ste.getLineNumber(), msg);
        if (LOGER) {
            Log.w(ste.getClassName(), logstr);
        }
        if (isSave) {
            appendLog(logstr);
        }
    }

    public static void e(String msg) {
        if (!LOGER && !isSave) return;
        final StackTraceElement[] stack = new Throwable().getStackTrace();
        final int i = 1;
        final StackTraceElement ste = stack[i];
        String logstr = String.format(FORMAT_STR, ste.getMethodName(), ste.getLineNumber(), msg);
        if (LOGER) {
            Log.e(ste.getClassName(), logstr);
        }
        if (isSave) {
            appendLog(logstr);
        }
    }

    public static void appendLog(String text) {
        getFileDir();
        if (logFile == null || !logFile.exists()) {
            try {
                logFile = new File(filesDir, MainApplication.getAppContext().getPackageName() + ".log");
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        BufferedWriter buf = null;
        try {
            buf = new BufferedWriter(new FileWriter(logFile, true));
            buf.append(text);
            buf.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (buf != null) {
                try {
                    buf.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static void enableLogToFile(boolean enable) {
        if (!enable) {
            deleteLogFile();
        }
    }

    public static File getFileDir() {
        if (filesDir == null) {
            filesDir = Environment.getExternalStorageDirectory();
        }
        if (filesDir == null) {
            filesDir = MainApplication.getAppContext().getFilesDir();
        }
        return filesDir;
    }

    public static File getLogFile() {
        return logFile;
    }

    public static void deleteLogFile() {
        if (logFile != null) {
            logFile.delete();
        }
    }

}
